package model.diagram.navigator;

import model.ActorPackActor;
import model.ActorsPackage;
import model.AssociationActorToSerie;
import model.EpisodesPackEpisode;
import model.EpisodesPackage;
import model.SeasonPackage;
import model.SeasonsPackSeason;
import model.SerieRole;
import model.SeriesPackSerie;
import model.SeriesPackage;
import model.diagram.edit.parts.ActorEditPart;
import model.diagram.edit.parts.ActorNameEditPart;
import model.diagram.edit.parts.ActorPackActorEditPart;
import model.diagram.edit.parts.ActorsPackageEditPart;
import model.diagram.edit.parts.AssociationActorToSerieEditPart;
import model.diagram.edit.parts.EpisodeEditPart;
import model.diagram.edit.parts.EpisodeNameEditPart;
import model.diagram.edit.parts.EpisodesPackEpisodeEditPart;
import model.diagram.edit.parts.EpisodesPackageEditPart;
import model.diagram.edit.parts.RoleEditPart;
import model.diagram.edit.parts.RoleNameEditPart;
import model.diagram.edit.parts.SeasonEditPart;
import model.diagram.edit.parts.SeasonNameEditPart;
import model.diagram.edit.parts.SeasonPackageEditPart;
import model.diagram.edit.parts.SeasonsPackSeasonEditPart;
import model.diagram.edit.parts.SerieEditPart;
import model.diagram.edit.parts.SerieNameEditPart;
import model.diagram.edit.parts.SerieRoleEditPart;
import model.diagram.edit.parts.SeriesPackSerieEditPart;
import model.diagram.edit.parts.SeriesPackageEditPart;
import model.diagram.edit.parts.WorkspaceEditPart;
import model.diagram.part.ModelDiagramEditorPlugin;
import model.diagram.part.ModelVisualIDRegistry;
import model.diagram.providers.ModelElementTypes;
import model.diagram.providers.ModelParserProvider;

import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class ModelNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		ModelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		ModelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof ModelNavigatorItem
				&& !isOwnView(((ModelNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof ModelNavigatorGroup) {
			ModelNavigatorGroup group = (ModelNavigatorGroup) element;
			return ModelDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof ModelNavigatorItem) {
			ModelNavigatorItem navigatorItem = (ModelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (ModelVisualIDRegistry.getVisualID(view)) {
		case SeriesPackSerieEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?model?SeriesPackSerie", ModelElementTypes.SeriesPackSerie_4001); //$NON-NLS-1$
		case SeasonEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?Season", ModelElementTypes.Season_2012); //$NON-NLS-1$
		case SeasonPackageEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?SeasonPackage", ModelElementTypes.SeasonPackage_2015); //$NON-NLS-1$
		case ActorPackActorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?model?ActorPackActor", ModelElementTypes.ActorPackActor_4002); //$NON-NLS-1$
		case WorkspaceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?model?Workspace", ModelElementTypes.Workspace_1000); //$NON-NLS-1$
		case EpisodesPackEpisodeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?model?EpisodesPackEpisode", ModelElementTypes.EpisodesPackEpisode_4004); //$NON-NLS-1$
		case SerieEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?Serie", ModelElementTypes.Serie_2011); //$NON-NLS-1$
		case EpisodeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?Episode", ModelElementTypes.Episode_2008); //$NON-NLS-1$
		case RoleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?Role", ModelElementTypes.Role_2009); //$NON-NLS-1$
		case SeasonsPackSeasonEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?model?SeasonsPackSeason", ModelElementTypes.SeasonsPackSeason_4003); //$NON-NLS-1$
		case SerieRoleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?model?SerieRole", ModelElementTypes.SerieRole_4005); //$NON-NLS-1$
		case SeriesPackageEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?SeriesPackage", ModelElementTypes.SeriesPackage_2002); //$NON-NLS-1$
		case AssociationActorToSerieEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?model?AssociationActorToSerie", ModelElementTypes.AssociationActorToSerie_4006); //$NON-NLS-1$
		case ActorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?Actor", ModelElementTypes.Actor_2010); //$NON-NLS-1$
		case ActorsPackageEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?ActorsPackage", ModelElementTypes.ActorsPackage_2001); //$NON-NLS-1$
		case EpisodesPackageEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?model?EpisodesPackage", ModelElementTypes.EpisodesPackage_2016); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = ModelDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& ModelElementTypes.isKnownElementType(elementType)) {
			image = ModelElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof ModelNavigatorGroup) {
			ModelNavigatorGroup group = (ModelNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof ModelNavigatorItem) {
			ModelNavigatorItem navigatorItem = (ModelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (ModelVisualIDRegistry.getVisualID(view)) {
		case SeriesPackSerieEditPart.VISUAL_ID:
			return getSeriesPackSerie_4001Text(view);
		case SeasonEditPart.VISUAL_ID:
			return getSeason_2012Text(view);
		case SeasonPackageEditPart.VISUAL_ID:
			return getSeasonPackage_2015Text(view);
		case ActorPackActorEditPart.VISUAL_ID:
			return getActorPackActor_4002Text(view);
		case WorkspaceEditPart.VISUAL_ID:
			return getWorkspace_1000Text(view);
		case EpisodesPackEpisodeEditPart.VISUAL_ID:
			return getEpisodesPackEpisode_4004Text(view);
		case SerieEditPart.VISUAL_ID:
			return getSerie_2011Text(view);
		case EpisodeEditPart.VISUAL_ID:
			return getEpisode_2008Text(view);
		case RoleEditPart.VISUAL_ID:
			return getRole_2009Text(view);
		case SeasonsPackSeasonEditPart.VISUAL_ID:
			return getSeasonsPackSeason_4003Text(view);
		case SerieRoleEditPart.VISUAL_ID:
			return getSerieRole_4005Text(view);
		case SeriesPackageEditPart.VISUAL_ID:
			return getSeriesPackage_2002Text(view);
		case AssociationActorToSerieEditPart.VISUAL_ID:
			return getAssociationActorToSerie_4006Text(view);
		case ActorEditPart.VISUAL_ID:
			return getActor_2010Text(view);
		case ActorsPackageEditPart.VISUAL_ID:
			return getActorsPackage_2001Text(view);
		case EpisodesPackageEditPart.VISUAL_ID:
			return getEpisodesPackage_2016Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getSeriesPackSerie_4001Text(View view) {
		SeriesPackSerie domainModelElement = (SeriesPackSerie) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 4001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSeason_2012Text(View view) {
		IParser parser = ModelParserProvider.getParser(
				ModelElementTypes.Season_2012,
				view.getElement() != null ? view.getElement() : view,
				ModelVisualIDRegistry.getType(SeasonNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5008); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSeasonPackage_2015Text(View view) {
		SeasonPackage domainModelElement = (SeasonPackage) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2015); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getActorPackActor_4002Text(View view) {
		ActorPackActor domainModelElement = (ActorPackActor) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 4002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getWorkspace_1000Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getEpisodesPackEpisode_4004Text(View view) {
		EpisodesPackEpisode domainModelElement = (EpisodesPackEpisode) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 4004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSerie_2011Text(View view) {
		IParser parser = ModelParserProvider.getParser(
				ModelElementTypes.Serie_2011,
				view.getElement() != null ? view.getElement() : view,
				ModelVisualIDRegistry.getType(SerieNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5007); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getEpisode_2008Text(View view) {
		IParser parser = ModelParserProvider.getParser(
				ModelElementTypes.Episode_2008,
				view.getElement() != null ? view.getElement() : view,
				ModelVisualIDRegistry.getType(EpisodeNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRole_2009Text(View view) {
		IParser parser = ModelParserProvider.getParser(
				ModelElementTypes.Role_2009,
				view.getElement() != null ? view.getElement() : view,
				ModelVisualIDRegistry.getType(RoleNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSeasonsPackSeason_4003Text(View view) {
		SeasonsPackSeason domainModelElement = (SeasonsPackSeason) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 4003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSerieRole_4005Text(View view) {
		SerieRole domainModelElement = (SerieRole) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 4005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSeriesPackage_2002Text(View view) {
		SeriesPackage domainModelElement = (SeriesPackage) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getAssociationActorToSerie_4006Text(View view) {
		AssociationActorToSerie domainModelElement = (AssociationActorToSerie) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 4006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getActor_2010Text(View view) {
		IParser parser = ModelParserProvider.getParser(
				ModelElementTypes.Actor_2010,
				view.getElement() != null ? view.getElement() : view,
				ModelVisualIDRegistry.getType(ActorNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getActorsPackage_2001Text(View view) {
		ActorsPackage domainModelElement = (ActorsPackage) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getEpisodesPackage_2016Text(View view) {
		EpisodesPackage domainModelElement = (EpisodesPackage) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ModelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2016); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return WorkspaceEditPart.MODEL_ID.equals(ModelVisualIDRegistry
				.getModelID(view));
	}

}
